package com.bolt.admin.module.sys.entity;

import com.bolt.support.spring.jpa.entity.BaseTreeEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.*;

import javax.persistence.*;
import javax.persistence.AccessType;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 米超
 *         权限控制资源，指对菜单、按钮、URL等访问控制点
 */
@Access(AccessType.FIELD)
@Entity
@Table(name = "SYS_PRIVILEGE")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@DynamicInsert
@DynamicUpdate
@Data
@NoArgsConstructor
public class PrivilegeEntity extends BaseTreeEntity<String,PrivilegeEntity> {

    private static final long serialVersionUID = 5139319086984812835L;

    public PrivilegeEntity(String id) {
        this.id = id;
    }

    @Id
    @Column(length = 40)
    @GeneratedValue(generator = "hibernate-uuid")
    @GenericGenerator(name = "hibernate-uuid", strategy = "org.hibernate.id.UUIDGenerator")
    private String id;

    private String code;

    @Column(nullable = false, length = 32)
    private String title;

    @Column(name = "MENU_ID", nullable = false, length = 11, unique = true)
    private Long menuId;

    public  enum PrivilegeTypeEnum {
        BUTTON,
        URL
    }
    @Enumerated(EnumType.STRING)
    @Column(length = 12, nullable = false)
    private PrivilegeTypeEnum type;


    @Column(name = "ORDER_RANK", length = 12, nullable = false)
    private Integer orderRank = 100;

    @OneToMany(mappedBy = "privilege", cascade = CascadeType.ALL, orphanRemoval = true)
    @JsonIgnore
    private List <RoleR2Privilege> roleR2Privileges = new ArrayList();

    @Transient
    private Boolean related;

}